package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;

import com.laoxuai.util.StringUtils;
import com.music.dao.SubscribeDao;
import com.music.pojo.Subscribe;
import com.music.service.SubscribeService;

import java.sql.SQLException;
import java.util.List;

@Service
public class SubscribeServiceImpl implements SubscribeService {

    @Autowired
    private SubscribeDao subscribeDao;

    @Override
    public int insert(Integer userId,Integer playlistId) {
        try {
            if(subscribeDao.getSubscribe(userId,playlistId)!=null){
                return -1;
            }
            Subscribe subscribe=new Subscribe();
            subscribe.setUserId(userId);
            subscribe.setPlaylistId(playlistId);
            return subscribeDao.insert(subscribe);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        try {
            return subscribeDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Subscribe subscribe) {
        try {
            return subscribeDao.update(subscribe);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Subscribe getByPrimaryKey(Integer id) {
        try {
            return subscribeDao.getByPrimaryKey(id);
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Subscribe> listPage(Subscribe subscribe, int page, int size) {
        try {
            return subscribeDao.listPage(subscribe, page, size);
        } catch (SQLException throwables) {
        throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Subscribe> listPageInfo(Subscribe subscribe, int page, int size) {
        try {
            return subscribeDao.listPageInfo(subscribe, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Subscribe> selectByExample(Subscribe subscribe) {
        Example example = createExmaple(subscribe);
        try {
            return subscribeDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Subscribe subscribe) {
        Example example = new Example(Subscribe.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(subscribe != null){
            if(!StringUtils.isEmpty(subscribe.getId())){
                criteria.andEqualTo("id", subscribe.getId());
            }
            if(!StringUtils.isEmpty(subscribe.getUserId())){
                criteria.andEqualTo("userId", subscribe.getUserId());
            }
            if(!StringUtils.isEmpty(subscribe.getPlaylistId())){
                criteria.andEqualTo("playlistId", subscribe.getPlaylistId());
            }
        }
        return example;
    }

    /**
     * Subscribe 构建查询对象
     * @param subscribe
     * @return
     */
    public Example createExample(Subscribe subscribe){
        Example example = new Example(Subscribe.class);
        Example.Criteria criteria = example.createCriteria();
        if(subscribe != null){
            if(!StringUtils.isEmpty(subscribe.getId())){
                criteria.andEqualTo("id", subscribe.getId());
            }
            if(!StringUtils.isEmpty(subscribe.getUserId())){
                criteria.andEqualTo("userId", subscribe.getUserId());
            }
            if(!StringUtils.isEmpty(subscribe.getPlaylistId())){
                criteria.andEqualTo("playlistId", subscribe.getPlaylistId());
            }
        }
        return example;
    }
}
